<template>
  <div>
    <el-col :span="12" :offset="5">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="角色名称" prop="roleid">
          <el-select v-model="form.roleid">
            <el-option
              v-for="(item, index) in roleListOption"
              :key="index"
              :label="item.rolename"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="管理员名称" prop="username">
          <el-input v-model="form.username"></el-input>
        </el-form-item>

        <el-form-item label="管理员密码" prop="password">
          <el-input v-model="form.password"></el-input>
        </el-form-item>

        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio :label="1">正常</el-radio>
            <el-radio :label="2">禁用</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item>
          <el-button
            v-if="$route.params.id > 0"
            type="primary"
            @click="submitForm('form')"
            >立即修改</el-button
          >
          <el-button v-else type="primary" @click="submitForm('form')"
            >立即创建</el-button
          >
          <el-button @click="resetForm('form')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-col>
  </div>
</template>
<script>
import { getAdminInfoOption } from "../../request";
export default {
  data() {
    return {
      form: {
        roleid: "",
        status: 1,
        username: "",
        password: "",
        menus: [],
      },
      rules: {
        roleid: [
          { required: true, message: "请输入角色名称", trigger: "blur" },
        ],
        username: [
          { required: true, message: "请输入管理员名称", trigger: "blur" },
        ],
      },
      roleListOption: [],
    };
  },
  created() {
    // 获取下拉菜单数据
    this.getData();
    if (this.$route.params.id > 0) {
      this.getDataById(this.$route.params.id);
    }
  },
  methods: {
    // 获取下拉菜单数据
    async getData() {
      let res = await getAdminInfoOption();
      console.log(res);
      this.roleListOption = res.data.list;
    },
    // 根据id获取角色详情
    getDataById(id) {
      this.http
        .get("/roleinfo", { params: { id } })
        .then((res) => {
          this.form = res.data.list;
          this.form.menus = res.data.list.menus.split(",");
          this.$ref.tree.setCheckedKeys(this.form.menus);
        })
        .catch((err) => {
          console.log(err);
        });
    },
    // 先通过验证在发送请求
    submitForm(formName) {
      // this.$refs[formName]通过refs找到这个表单，然后验证
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.$route.params.id > 0) {
            this.form.id = this.$route.params.id;

            this.http
              .post("/roleedit", this.form)
              .then((res) => {
                this.$router.go(-1);
              })
              .catch((err) => {
                console.log(err);
              });
          } else {
            // 新增
            this.http
              .post("/useradd", this.form)
              .then((res) => {
                this.$router.go(-1);
              })
              .catch((err) => {
                console.log(err);
              });
          }
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>